********************************************************************************
**	PURPOSE: Put together the results of the calibration test including against null = 1, create T10, AT8
**							
**	INPUTS: HTE_ficoscore08_e1_df.dta
**			HTE_ficoscore08_e2_df.dta
**			HTE_ficoscore08_e3_df.dta
**			HTE_scoredf_e1_df.dta
**			HTE_scoredf_e2_df.dta
**			HTE_scoredf_e3_df.dta
**	
**	OUTPUTS: HTE_ficoscore08_alle.dta
**			 HTE_scoredf_alle.dta
**			 ITE_CATEmean_ficoscore08_all_notca - Table 10 
**			 ITE_CATEmean_ficsocre08full_all_notca - Table 10
**			 ITE_CATEmean_scoredf_all_notca - Appendix Table 8
**			 ITE_CATEmean_scoredf_all_notca - Appendix Table 8 
**			 
**  CREATED/EDITED BY: Leah Kim, Hasan Ahamed
**
**	DATE LAST EDITED: 4/8/2022, 2/28/2023
********************************************************************************
clear all 

local depvar ficoscore08 scoredf

foreach dep of local depvar {

use "$adta/HTE_`dep'_e1_df.dta", clear
gen endline = 1
append using "$adta/HTE_`dep'_e2_df.dta"
replace endline = 2 if missing(endline)
append using "$adta/HTE_`dep'_e3_df.dta"
replace endline = 3 if missing(endline)

* Put all endlines together 

	sort surveyid
	distinct surveyid 
	egen countid = count(surveyid), by(surveyid) 
	tab countid // mostly three copies
	collapse t1_s1_pred htca ltca mtca enc op18 flag_cblgroup flag_extragroup, by(surveyid) // take the averages of the ITEs across three endlines
	sum t1_s1_pred

	save "$adta/HTE_`dep'_alle.dta", replace

	foreach f in "full" "" {
		if "`f'" == "full" {
		} 
		else {
		keep if op18 == 1
		} 
	
		ttest t1_s1_pred, by(enc)

		*inputting results into a matrix to output them
		mat results = J(3,3,.)
		mat results[1,1] = r(mu_1)
		mat results[2,1] = r(sd_1) / sqrt(r(N_1))
		mat results[3,1] = r(N_1)

		mat results[1,2] = r(mu_2)
		mat results[2,2] = r(sd_2) / sqrt(r(N_2))
		mat results[3,2] = r(N_2)
		
		mat results[1,3] = r(p)
		
		putexcel set "$outputtables/ITE_CATEmean_`dep'`f'_all_notca.xlsx", modify
		putexcel A1 = matrix(results) 
	}
}

**EOF**
